#!/usr/bin/bash

# sudo apt-get install lshw
# sudo apt-get install smartmontools
# sudo apt-get install dmidecode
# sudo apt-get install hwinfo
# sudo apt-get install upower

# 创建目录

devicedir=deviceInfo

mkdir -p $devicedir

function get_neofetch_info() {
    neofetch --stdout > $devicedir/neofetch.txt
}

# dmidecode
function get_dmidecode_info() {
    mkdir -p $devicedir/dmidecode-class
    dmidecode               > $devicedir/dmidecode.txt
    dmidecode -t bios       > $devicedir/dmidecode-class/dmidecode-bios.txt
    dmidecode -t system     > $devicedir/dmidecode-class/dmidecode-system.txt
    dmidecode -t baseboard  > $devicedir/dmidecode-class/dmidecode-baseboard.txt
    dmidecode -t chassis    > $devicedir/dmidecode-class/dmidecode-chassis.txt
    dmidecode -t processor  > $devicedir/dmidecode-class/dmidecode-processor.txt
    dmidecode -t memory     > $devicedir/dmidecode-class/dmidecode-memory.txt
    dmidecode -t cache      > $devicedir/dmidecode-class/dmidecode-cache.txt
    dmidecode -t connector  > $devicedir/dmidecode-class/dmidecode-connector.txt
    dmidecode -t slot       > $devicedir/dmidecode-class/dmidecode-slot.txt
    man dmidecode           > $devicedir/dmidecode-class/man-dmidecode.txt

    mkdir -p $devicedir/dmidecode-all
    dmidecode -t 0   > $devicedir/dmidecode-all/dmidecode-0-bios.txt
    dmidecode -t 1   > $devicedir/dmidecode-all/dmidecode-1-system.txt
    dmidecode -t 2   > $devicedir/dmidecode-all/dmidecode-2-baseboard.txt
    dmidecode -t 3   > $devicedir/dmidecode-all/dmidecode-3-chassis.txt
    dmidecode -t 4   > $devicedir/dmidecode-all/dmidecode-4-processor.txt
    dmidecode -t 5   > $devicedir/dmidecode-all/dmidecode-5-memory_controller.txt
    dmidecode -t 6   > $devicedir/dmidecode-all/dmidecode-6-memory_module.txt
    dmidecode -t 7   > $devicedir/dmidecode-all/dmidecode-7-cache.txt
    dmidecode -t 8   > $devicedir/dmidecode-all/dmidecode-8-port_connector.txt
    dmidecode -t 9   > $devicedir/dmidecode-all/dmidecode-9-system_slots.txt
    dmidecode -t 10  > $devicedir/dmidecode-all/dmidecode-10-on_board_devices.txt
    dmidecode -t 11  > $devicedir/dmidecode-all/dmidecode-11-oem_strings.txt
    dmidecode -t 12  > $devicedir/dmidecode-all/dmidecode-12-system_configuration_options.txt
    dmidecode -t 13  > $devicedir/dmidecode-all/dmidecode-13-bios_language.txt
    dmidecode -t 14  > $devicedir/dmidecode-all/dmidecode-14-group_associations.txt
    dmidecode -t 15  > $devicedir/dmidecode-all/dmidecode-15-system_event_log.txt
    dmidecode -t 16  > $devicedir/dmidecode-all/dmidecode-16-physical_memory_array.txt
    dmidecode -t 17  > $devicedir/dmidecode-all/dmidecode-17-memory_device.txt
    dmidecode -t 18  > $devicedir/dmidecode-all/dmidecode-18-32-bit_memory_error.txt
    dmidecode -t 19  > $devicedir/dmidecode-all/dmidecode-19-memory_array_mapped_address.txt
    dmidecode -t 20  > $devicedir/dmidecode-all/dmidecode-20-memory_device_mapped_address.txt
    dmidecode -t 21  > $devicedir/dmidecode-all/dmidecode-21-built-in_pointing_device.txt
    dmidecode -t 22  > $devicedir/dmidecode-all/dmidecode-22-portable_battery.txt
    dmidecode -t 23  > $devicedir/dmidecode-all/dmidecode-23-system_reset.txt
    dmidecode -t 24  > $devicedir/dmidecode-all/dmidecode-24-hardware_security.txt
    dmidecode -t 25  > $devicedir/dmidecode-all/dmidecode-25-system_power_controls.txt
    dmidecode -t 26  > $devicedir/dmidecode-all/dmidecode-26-voltage_probe.txt
    dmidecode -t 27  > $devicedir/dmidecode-all/dmidecode-27-cooling_device.txt
    dmidecode -t 28  > $devicedir/dmidecode-all/dmidecode-28-temperature_probe.txt
    dmidecode -t 29  > $devicedir/dmidecode-all/dmidecode-29-electrical_current_probe.txt
    dmidecode -t 30  > $devicedir/dmidecode-all/dmidecode-30-out-of-band_remote_access.txt
    dmidecode -t 31  > $devicedir/dmidecode-all/dmidecode-31-boot_integrity_services.txt
    dmidecode -t 32  > $devicedir/dmidecode-all/dmidecode-32-system_boot.txt
    dmidecode -t 33  > $devicedir/dmidecode-all/dmidecode-33-64-bit_memory_error.txt
    dmidecode -t 34  > $devicedir/dmidecode-all/dmidecode-34-management_device.txt
    dmidecode -t 35  > $devicedir/dmidecode-all/dmidecode-35-management_device_component.txt

    mkdir -p $devicedir/dmidecode-keywords
    dmidecode -s bios-vendor              > $devicedir/dmidecode-keywords/bios-vendor.txt
    dmidecode -s bios-version             > $devicedir/dmidecode-keywords/bios-version.txt
    dmidecode -s bios-release-date        > $devicedir/dmidecode-keywords/bios-release-date.txt
    dmidecode -s system-manufacturer      > $devicedir/dmidecode-keywords/system-manufacturer.txt
    dmidecode -s system-product-name      > $devicedir/dmidecode-keywords/system-product-name.txt
    dmidecode -s system-version           > $devicedir/dmidecode-keywords/system-version.txt
    dmidecode -s system-serial-number     > $devicedir/dmidecode-keywords/system-serial-number.txt
    dmidecode -s system-uuid              > $devicedir/dmidecode-keywords/system-uuid.txt
    dmidecode -s system-family            > $devicedir/dmidecode-keywords/system-family.txt
    dmidecode -s baseboard-manufacturer   > $devicedir/dmidecode-keywords/baseboard-manufacturer.txt
    dmidecode -s baseboard-product-name   > $devicedir/dmidecode-keywords/baseboard-product-name.txt
    dmidecode -s baseboard-version        > $devicedir/dmidecode-keywords/baseboard-version.txt
    dmidecode -s baseboard-serial-number  > $devicedir/dmidecode-keywords/baseboard-serial-number.txt
    dmidecode -s baseboard-asset-tag      > $devicedir/dmidecode-keywords/baseboard-asset-tag.txt
    dmidecode -s chassis-manufacturer     > $devicedir/dmidecode-keywords/chassis-manufacturer.txt
    dmidecode -s chassis-type             > $devicedir/dmidecode-keywords/chassis-type.txt
    dmidecode -s chassis-version          > $devicedir/dmidecode-keywords/chassis-version.txt
    dmidecode -s chassis-serial-number    > $devicedir/dmidecode-keywords/chassis-serial-number.txt
    dmidecode -s chassis-asset-tag        > $devicedir/dmidecode-keywords/chassis-asset-tag.txt
    dmidecode -s processor-family         > $devicedir/dmidecode-keywords/processor-family.txt
    dmidecode -s processor-manufacturer   > $devicedir/dmidecode-keywords/processor-manufacturer.txt
    dmidecode -s processor-version        > $devicedir/dmidecode-keywords/processor-version.txt
    dmidecode -s processor-frequency      > $devicedir/dmidecode-keywords/processor-frequency.txt
}

# /proc
function get_proc_info() {
    # /proc
    mkdir -p $devicedir/proc
    # 系统信息
    cat /proc/version > $devicedir/proc/version.txt
    cat /proc/cpuinfo > $devicedir/proc/cpuinfo.txt

    # /proc/bus
    mkdir -p $devicedir/proc/bus
    # input 输入设备信息
    cat /proc/bus/input/devices > $devicedir/proc/bus/input-devices.txt
    cat /proc/bus/input/handlers > $devicedir/proc/bus/input-handlers.txt
    # pci 设备信息
    cat /proc/bus/pci/devices    > $devicedir/proc/bus/pci-devices.txt

    # /proc/asound
    mkdir -p $devicedir/proc/asound
    # /proc/asound
    cat /proc/asound/devices > $devicedir/proc/asound/devices.txt
    cat /proc/asound/modules > $devicedir/proc/asound/modules.txt

    # /proc/sys/
    mkdir -p $devicedir/proc/sys
    # kernel
    for file in `ls /proc/sys/kernel`; do
        test -f /proc/sys/kernel/$file && cat /proc/sys/kernel/$file > $devicedir/proc/sys/kernel-$file.txt
    done
}

# /sys/kernel/debug/usb/devices
function get_sys_info() {
    # /sys
    mkdir -p $devicedir/sys

    # /sys/kernel/debug
    mkdir -p $devicedir/sys/kernel/debug/usb
    cat /sys/kernel/debug/usb/devices > $devicedir/sys/kernel/debug/usb/devices.txt
}

function get_lsx_info() {

    # cpu信息
    lscpu > $devicedir/lscpu.txt

    # pci 设备信息
    lspci       > $devicedir/lspci.txt
    lspci -v    > $devicedir/lspci-v.txt
    lspci -vv   > $devicedir/lspci-vv.txt
    lspci -m    > $devicedir/lspci-m.txt
    lspci -mv   > $devicedir/lspci-mv.txt
    lspci -tv   > $devicedir/lspci-tv.txt
  
    mkdir -p $devicedir/lsusb
    # usb 设备信息
    for item in `lsusb | awk -F' ' '{ print $6 }'`;
    do
        echo "${item}"
        lsusb -d ${item} -v > $devicedir/lsusb/${item}.txt
    done

    mkdir -p $devicedir/lspci
    # pci 设备信息
    for item in `lspci | awk -F' ' '{ print $1 }'`;
    do
        echo "${item}"
        lspci -s ${item} -v > $devicedir/lspci/${item}.txt
    done
}

# lshw
function get_lshw_info() {
    # 硬件信息
    lshw > $devicedir/lshw.txt

    mkdir -p $devicedir/lshw

    lshw -c bridge        > $devicedir/lshw/lshw-bridge.txt
    lshw -c bus           > $devicedir/lshw/lshw-bus.txt
    lshw -c communication > $devicedir/lshw/lshw-communication.txt
    lshw -c disk          > $devicedir/lshw/lshw-disk.txt
    lshw -c display       > $devicedir/lshw/lshw-display.txt
    lshw -c generic       > $devicedir/lshw/lshw-generic.txt
    lshw -c memory        > $devicedir/lshw/lshw-memory.txt
    lshw -c multimedia    > $devicedir/lshw/lshw-multimedia.txt
    lshw -c network       > $devicedir/lshw/lshw-network.txt
    lshw -c power         > $devicedir/lshw/lshw-power.txt
    lshw -c processor     > $devicedir/lshw/lshw-processor.txt
    lshw -c storage       > $devicedir/lshw/lshw-storage.txt
    lshw -c system        > $devicedir/lshw/lshw-system.txt
}

# smartctl
function get_smartctl_info() {
    # 硬盘
    for block in `ls /dev/block/ | awk -F ':' '{print $1}' | uniq`;
    do
        devname=$(basename `realpath "/dev/block/${block}:0"`)
        smartctl --all /dev/${devname} > $devicedir/smartctl-${devname}-${block}.txt
    done
}

# 关于hwinfo各种硬件信息
function get_hwinfo() {
    mkdir -p $devicedir/hwinfo
    # all
    hwinfo --all          > $devicedir/hwinfo/hwinfo-all.txt 
    # arch
    hwinfo --arch         > $devicedir/hwinfo/hwinfo-arch.txt
    # bios
    hwinfo --bios         > $devicedir/hwinfo/hwinfo-bios.txt
    # block
    hwinfo --block        > $devicedir/hwinfo/hwinfo-block.txt
    # bluetooth
    hwinfo --bluetooth    > $devicedir/hwinfo/hwinfo-bluetooth.txt
    # braille
    hwinfo --braille      > $devicedir/hwinfo/hwinfo-braille.txt
    # bridge
    hwinfo --bridge       > $devicedir/hwinfo/hwinfo-bridge.txt
    # camera
    hwinfo --camera       > $devicedir/hwinfo/hwinfo-camera.txt
    # cdrom
    hwinfo --cdrom        > $devicedir/hwinfo/hwinfo-cdrom.txt
    # chipcard
    hwinfo --chipcard     > $devicedir/hwinfo/hwinfo-chipcard.txt
    # cpu
    hwinfo --cpu          > $devicedir/hwinfo/hwinfo-cpu.txt
    # disk
    hwinfo --disk         > $devicedir/hwinfo/hwinfo-disk.txt
    # dsl
    hwinfo --dsl          > $devicedir/hwinfo/hwinfo-dsl.txt
    # dvb
    hwinfo --dvb          > $devicedir/hwinfo/hwinfo-dvb.txt
    # fingerprint
    hwinfo --fingerprint  > $devicedir/hwinfo/hwinfo-fingerprint.txt
    # floppy
    hwinfo --floppy       > $devicedir/hwinfo/hwinfo-floppy.txt
    # framebuffer
    hwinfo --framebuffer  > $devicedir/hwinfo/hwinfo-framebuffer.txt
    # gfxcard
    hwinfo --gfxcard      > $devicedir/hwinfo/hwinfo-gfxcard.txt
    # hub
    hwinfo --hub          > $devicedir/hwinfo/hwinfo-hub.txt
    # ide
    hwinfo --ide          > $devicedir/hwinfo/hwinfo-ide.txt
    # isapnp
    hwinfo --isapnp       > $devicedir/hwinfo/hwinfo-isapnp.txt
    # isdn
    hwinfo --isdn         > $devicedir/hwinfo/hwinfo-isdn.txt
    # joystick
    hwinfo --joystick     > $devicedir/hwinfo/hwinfo-joystick.txt
    # keyboard
    hwinfo --keyboard     > $devicedir/hwinfo/hwinfo-keyboard.txt
    # memory
    hwinfo --memory       > $devicedir/hwinfo/hwinfo-memory.txt
    # mmc-ctrl
    hwinfo --mmc-ctrl     > $devicedir/hwinfo/hwinfo-mmc-ctrl.txt
    # modem
    hwinfo --modem        > $devicedir/hwinfo/hwinfo-modem.txt
    # monitor
    hwinfo --monitor      > $devicedir/hwinfo/hwinfo-monitor.txt
    # mouse
    hwinfo --mouse        > $devicedir/hwinfo/hwinfo-mouse.txt
    # netcard
    hwinfo --netcard      > $devicedir/hwinfo/hwinfo-netcard.txt
    # network
    hwinfo --network      > $devicedir/hwinfo/hwinfo-network.txt
    # partition
    hwinfo --partition    > $devicedir/hwinfo/hwinfo-partition.txt
    # pci
    hwinfo --pci          > $devicedir/hwinfo/hwinfo-pci.txt
    # pcmcia
    hwinfo --pcmcia       > $devicedir/hwinfo/hwinfo-pcmcia.txt
    # pcmcia-ctrl
    hwinfo --pcmcia-ctrl  > $devicedir/hwinfo/hwinfo-pcmcia-ctrl.txt
    # pppoe
    hwinfo --pppoe        > $devicedir/hwinfo/hwinfo-pppoe.txt
    # printer
    hwinfo --printer      > $devicedir/hwinfo/hwinfo-printer.txt
    # redasd
    hwinfo --redasd       > $devicedir/hwinfo/hwinfo-redasd.txt
    # reallyall
    hwinfo --reallyall    > $devicedir/hwinfo/hwinfo-reallyall.txt
    # scanner
    hwinfo --scanner      > $devicedir/hwinfo/hwinfo-scanner.txt
    # scsi
    hwinfo --scsi         > $devicedir/hwinfo/hwinfo-scsi.txt
    # smp
    hwinfo --smp          > $devicedir/hwinfo/hwinfo-smp.txt
    # sound
    hwinfo --sound        > $devicedir/hwinfo/hwinfo-sound.txt
    # storage-ctrl
    hwinfo --storage-ctrl > $devicedir/hwinfo/hwinfo-storage-ctrl.txt
    # sys
    hwinfo --sys          > $devicedir/hwinfo/hwinfo-sys.txt
    # tape
    hwinfo --tape         > $devicedir/hwinfo/hwinfo-tape.txt
    # tv
    hwinfo --tv           > $devicedir/hwinfo/hwinfo-tv.txt
    # uml
    hwinfo --uml          > $devicedir/hwinfo/hwinfo-uml.txt
    # usb
    hwinfo --usb          > $devicedir/hwinfo/hwinfo-usb.txt
    # usb-ctrl
    hwinfo --usb-ctrl     > $devicedir/hwinfo/hwinfo-usb-ctrl.txt
    # vbe
    hwinfo --vbe          > $devicedir/hwinfo/hwinfo-vbe.txt
    # wlan
    hwinfo --wlan         > $devicedir/hwinfo/hwinfo-wlan.txt
    # xen
    hwinfo --xen          > $devicedir/hwinfo/hwinfo-xen.txt
    # zip
    hwinfo --zip          > $devicedir/hwinfo/hwinfo-zip.txt
}

function get_x11_info() {
    # 屏幕xrandr信息 - 需要用户Session
    xrandr --verbose > $devicedir/xrandr.txt
}

function get_usb_devices() {
    # usb 设备信息
    usb-devices > $devicedir/usb-devcies.txt
}

# upower
function get_upower_info() {
    mkdir -p $devicedir/upower
    upower --dump > $devicedir/upower-dump.txt
    upower --enumerate > $devicedir/upower-enumerate.txt

    for enumerate in `upower --enumerate`; do
        upower --show-info $enumerate > $devicedir/upower/$(echo $enumerate | awk -F/ '{print $6}').txt
    done
}

# 获取由这些工具提供的信息
# neofetch,dmidecode,proc,ls[hw|pci|usb|cpu],smartctl,hwinfo,xrandr,
get_neofetch_info
get_dmidecode_info
get_proc_info
get_sys_info
get_lsx_info
get_lshw_info
get_smartctl_info
get_hwinfo
get_x11_info
get_usb_devices
get_upower_info


# 蓝牙设备信息
hciconfig -a > $devicedir/hciconfig.txt

# lsb_release 信息
lsb_release -a > $devicedir/lsb_release.txt

# sdptool browse local
dmesg > $devicedir/dmesg.txt

# 收集整个设备目录树信息
tree /dev > $devicedir/tree-dev.txt
tree /sys > $devicedir/tree-sys.txt

find /dev > $devicedir/find-dev.txt
find /sys > $devicedir/find-sys.txt

# 打包整个设备信息
tar zcvf $devicedir.tar.gz $devicedir

# 
chown 1000:1000 -R $devicedir
chown 1000:1000 $devicedir.tar.gz